<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
class database {
	var $_sql= '';
	var $_connection= '';
	var $_cursor = null;
	function database() 
	{
		global $database_host,$database_user,$database_password,$database_name;
		$this->_connection = @mysql_connect( $database_host,$database_user,$database_password);
		//$this->_connection = @mysql_connect ("localhost","root","");
		if (!$this->_connection ) 
		{
			die( "Trang Web này đang tạm ngừng để bảo dưỡng.<br /> Và sẽ hoạt động trở lại sớm nhất. (1.1)");
		}
		mysql_query("SET NAMES utf8");
		$db=$database_name;
		if ($database_name != '' && !mysql_select_db( $db, $this->_connection )) 
		{
			die ( "Trang Web này đang tạm ngừng để bảo dưỡng.<br /> Và sẽ hoạt động trở lại sớm nhất. (1.2)" );
		}
		
	}

	function setQuery( $sql) 
	{
		$this->_sql = $sql;
	}

	function query() 
	{	
		$this->_cursor = mysql_query( $this->_sql, $this->_connection );
		return $this->_cursor;
	}

	/**
	* This method loads the first field of the first row returned by the query.
	*
	* @return The value returned in the query or null if the query failed.
	*/
	function loadRow() {
		if (!($cur = $this->query())) {
			return null;
		}
		$ret = null;
		if ($row = mysql_fetch_row( $cur )) {
			$ret = $row[0];
		}
		mysql_free_result( $cur );
		return $ret;
	}
	/**
	* Load sum rows in query
	*/
	function getNumRows() {
		return mysql_num_rows($this->_cursor);
	}
	/**
	* Load an array of single field results into an array
	*/
	function loadArrayField($numinarray = 0) {
		if (!($cur = $this->query())) {
			return null;
		}
		$array = array();
		while ($row = mysql_fetch_row( $cur )) {
			$array[] = $row[$numinarray];
		}
		mysql_free_result( $cur );
		return $array;
	}
	/**
	* Load a  list of database rows
	*/
	function loadAllRow() {
		if (!($cur = $this->query())) {
			return null;
		}
		$array = array();
		while ($row = mysql_fetch_assoc( $cur )) 
		{
			// ketqua = mang ket qua
			$array[] = $row;
		}
		mysql_free_result( $cur );
		return $array;
	}
	/**
	* Load a rows
	*/
	function loadOneRow() {
		if (!($cur = $this->query())) {
			return null;
		}
		while ($row = mysql_fetch_assoc( $cur )) 
		{
			// ketqua = mang ket qua
			return $row;
		}
		return null;
	}
	/**
	* Load a list of database rows (numeric column indexing)
	* @param string The field name of a primary key
	* @return array If <var>key</var> is empty as sequential list of returned records.
	* If <var>key</var> is not empty then the returned array is indexed by the value
	* the database key.  Returns <var>null</var> if the query fails.
	*/
	function loadRowList( $key='' ) 
	{
		if (!($cur = $this->query())) {
			return null;
		}
		$array = array();
		while ($row = mysql_fetch_array( $cur )) {
			if ($key) {
				$array[$row[$key]] = $row;
			} else {
				$array[] = $row;
			}
		}
		mysql_free_result( $cur );
		return $array;
	}
	function disconnect() 
	{
		mysql_close( $this->_connection );
	}
	function escape_data($data)
	{
		if(ini_get('magic_quotes_gpc'))
		{	$data = stripslashes($data);
		}
		return mysql_real_escape_string($data);
	}
}
?>